package com.jy.wms.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jy.wms.common.PinyinUtils;
import com.jy.wms.common.annotions.BaseServiceDescribe;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.OutstockDetailed;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.dao.WmsOutstockDetailedReportMapper;
import com.jy.wms.pojo.OutstockDetailedReport;
import com.jy.wms.pojo.WmsOrderChannel;
import com.jy.wms.pojo.WmsOwnerOrdertype;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;

/**
 * Created by syh on 2018/4/11.
 */
@Service
public class WmsOutstockDetailedReportService extends BaseService<WmsOutstockDetailedReportMapper,OutstockDetailedReport> {
    @Autowired
    private WmsInstockDetailedReportService wmsInstockDetailedReportService;
    /**
     * 出库明细报表
     * @param entity
     * @return
     * @throws ServiceException
     */
    @BaseServiceDescribe(moduleName = "出库明细报表",btnName = "分页查询出库明细报表")
    public Map<String, Object> baseSelectPage(OutstockDetailedReport entity) throws ServiceException {
        try{
            Map map1 = new HashMap();
            Page<OutstockDetailedReport> pageList = new Page<OutstockDetailedReport>();
            List<Map<String, Object>> list = new ArrayList<>();
            if(entity.getCjsj_start()!=null && !"".equals(entity.getCjsj_start()) && entity.getDdqdid()!=null && !"".equals(entity.getDdqdid())){
                //查询订单渠道id以及下属所有子节点
                List<String> ddqdidList1=getDqid(entity.getCkid(),entity.getHzid(),entity.getDdqdid());
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Calendar c = Calendar.getInstance();
                c.setTime(new Date(Long.valueOf(entity.getCjsj_start())));
                c.set(Calendar.HOUR_OF_DAY, 00);
                c.set(Calendar.MINUTE, 00);
                c.set(Calendar.SECOND, 00);
                entity.setCjsj_start(sdf.format(c.getTime()));

                c.setTime(new Date(Long.valueOf(entity.getCjsj_end())));
                c.set(Calendar.HOUR_OF_DAY, 23);
                c.set(Calendar.MINUTE, 59);
                c.set(Calendar.SECOND, 59);
                entity.setCjsj_end(sdf.format(c.getTime()));
                //分页查询
                PageHelper.startPage(entity.getPage(),entity.getRows());
                pageList=(Page<OutstockDetailedReport>)dao.baseSelectPage(entity,ddqdidList1);
                for(OutstockDetailedReport outstockDetailedReport:pageList){
                    getGongtong(entity, list, outstockDetailedReport);
                }
           }
            map1.put("contentList", list);
            map1.put("totals", pageList.getTotal());
            return map1;
        }catch(Exception e){
            throw  new ServiceException("查询出库明细报表失败",e);
        }
    }

    /**
     * 分页查询和导出查询提取共同部分代码
     * @param entity
     * @param list
     * @param outstockDetailedReport
     */
    public void getGongtong(OutstockDetailedReport entity, List<Map<String, Object>> list, OutstockDetailedReport outstockDetailedReport) throws  Exception{
        Map map;
        map = new HashMap();
        //出库汇总
        String ckhz=dao.getCkhz(outstockDetailedReport,entity.getCjsj_start(),entity.getCjsj_end(),entity.getHzid(),entity.getCkid());
        //期初库存上架数量
        SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd");
        String mon =d.format(DateFormat.getDateInstance().parse(entity.getCjsj_start()));
        String mon1 = d.format(DateFormat.getDateInstance().parse(entity.getCjsj_end()));
        //期初库存上架数量
        Integer qckc= dao.getQckc(outstockDetailedReport,mon);
        Integer qmkc= dao.getQmkc(outstockDetailedReport,mon1);
        //订单类型
        List<Map<String, Object>> list3=dao.selectDdlx(outstockDetailedReport.getCpbh(),entity.getHzid(),entity.getCkid(),entity.getCjsj_start(),entity.getCjsj_end());
        for(Map map3:list3){
            if(map3!=null){
                //订单类型的拼音
                String ddlx= PinyinUtils.ToPinyin(map3.get("ddlx").toString());
                map.put(ddlx,map3.get("picksl").toString());
            }
        }
        map.put("cpid",outstockDetailedReport.getCpid());
        map.put("chanpinbianhao",outstockDetailedReport.getCpbh());
        map.put("chanpinfenlei",outstockDetailedReport.getCpfl());
        map.put("chanpinmingcheng",outstockDetailedReport.getCpmc());
        map.put("dingdanqudao",outstockDetailedReport.getDdqdmc());
        map.put("guige",outstockDetailedReport.getGg());
        map.put("danwei",outstockDetailedReport.getDw());
        map.put("qichukucun",qckc);
        map.put("chukuhuizong",ckhz);
        map.put("qimokucun",qmkc);
        list.add(map);
    }
    /**
     * (分页查询)表头创建
     * @param hzid
     * @param ckid
     * @return
     */
    public List<Map<String, Object>> listHeader(String hzid, String ckid) {
        List<Map<String, Object>> mapList = new ArrayList<>();
        Map map = null;
        //中文汉字产品编号
        String cpbh= OutstockDetailed.cpbh.getType();
        //中文首字母
        String cpbh2=PinyinUtils.ToPinyin(cpbh);
        map = wmsInstockDetailedReportService.header(map, cpbh, cpbh2, "false");
        mapList.add(map);
        //产品分类
        String cpfl=OutstockDetailed.cpfl.getType();
        String cpfl2=PinyinUtils.ToPinyin(cpfl);
        map = wmsInstockDetailedReportService.header(map, cpfl, cpfl2, "false");
        mapList.add(map);
        //产品名称
        String cpmc=OutstockDetailed.cpmc.getType();
        String cpmc2=PinyinUtils.ToPinyin(cpmc);
        map = wmsInstockDetailedReportService.header(map, cpmc, cpmc2, "false");
        mapList.add(map);
        //订单渠道
        String ddqdmc=OutstockDetailed.ddqdmc.getType();
        String ddqdmc2=PinyinUtils.ToPinyin(ddqdmc);
        map = wmsInstockDetailedReportService.header(map, ddqdmc, ddqdmc2, "true");
        mapList.add(map);
        //规格
        String gg=OutstockDetailed.gg.getType();
        String gg2=PinyinUtils.ToPinyin(gg);
        map = wmsInstockDetailedReportService.header(map, gg, gg2, "false");
        mapList.add(map);
        //单位描述(主单位的)
        String dw=OutstockDetailed.dw.getType();
        String dw2=PinyinUtils.ToPinyin(dw);
        map = wmsInstockDetailedReportService.header(map, dw, dw2, "false");
        mapList.add(map);
        //期初库存
        String qckc=OutstockDetailed.qckc.getType();
        String qckc2=PinyinUtils.ToPinyin(qckc);
        map = wmsInstockDetailedReportService.header(map, qckc, qckc2, "false");
        mapList.add(map);
        //出库类型
        List<WmsOwnerOrdertype> listType=dao.selectType(hzid,ckid);
        for(WmsOwnerOrdertype wmsOwnerOrdertype:listType){
            String ddlx=wmsOwnerOrdertype.getDdlx();
            String ddlx2=PinyinUtils.ToPinyin(ddlx);
            map = wmsInstockDetailedReportService.header(map, ddlx, ddlx2, "false");
            mapList.add(map);
        }
        //出库汇总
        String ckhz=OutstockDetailed.ckhz.getType();
        String ckhz2=PinyinUtils.ToPinyin(ckhz);
        map = wmsInstockDetailedReportService.header(map, ckhz, ckhz2, "false");
        mapList.add(map);
        //期末库存
        String qmkc=OutstockDetailed.qmkc.getType();
        String qmkc2=PinyinUtils.ToPinyin(qmkc);
        map = wmsInstockDetailedReportService.header(map, qmkc, qmkc2, "false");
        mapList.add(map);
        return mapList;
    }
    /**
     * 订单渠道名称模糊查询
     * @param mc
     * @param hzid
     * @param ckid
     * @return
     * @throws ServiceException
     */
    @BaseServiceDescribe(moduleName = "出库明细管理",btnName = "根据订单渠道名称模糊查询")
    public List<WmsOrderChannel> getDdmc(String mc,String hzid,String ckid) throws ServiceException{
        try {
            List<WmsOrderChannel> list = dao.getDdmc(mc,hzid,ckid);
            return list;
        } catch (Exception e) {
            throw new ServiceException("根据订单渠道名称模糊查询信息",e);
        }
    }

    /**
     * 查询订单渠道id以及下属所有子节点
     */
    List<String> childMenu=new ArrayList<String>();
    public List<String> getDqid( String ckid,String hzid,String pid) {
        childMenu.clear();
        childMenu.add(pid);
        //查所有订单渠道id,
       List<WmsOrderChannel> dqidList= dao.getDqid(ckid,hzid,pid);
        for (WmsOrderChannel wmsOrderChannel:dqidList) {
            // 根据传入的某个父节点ID,遍历该父节点的所有子节点
            wmsOrderChannel.setCkid(ckid);
            wmsOrderChannel.setSshzid(hzid);
            childMenu.add(wmsOrderChannel.getId());
            recursionFn(wmsOrderChannel);
        }
        return childMenu;
    }
    public void recursionFn(WmsOrderChannel wmsOrderChannel) {
        List<WmsOrderChannel> dqidList= dao.getDqid(wmsOrderChannel.getCkid(),wmsOrderChannel.getSshzid(),wmsOrderChannel.getId());
        if(dqidList.size()>0){
            for(WmsOrderChannel wmsOrderChannel1:dqidList){
                childMenu.add(wmsOrderChannel1.getId());
                recursionFn(wmsOrderChannel1);
            }
        }
    }
    /**
     * 报表导出查询
     * @param entity
     * @return
     */
    public List<Map<String, Object>> getOutstockExport(OutstockDetailedReport entity) throws Exception{
        List<Map<String, Object>> list = new ArrayList<>();
        if(entity.getCjsj_start()!=null && !"".equals(entity.getCjsj_start()) && entity.getDdqdid()!=null && !"".equals(entity.getDdqdid())){
            List<String> ddqdidList1=getDqid(entity.getCkid(),entity.getHzid(),entity.getDdqdid());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar c = Calendar.getInstance();
            c.setTime(new Date(Long.valueOf(entity.getCjsj_start())));
            c.set(Calendar.HOUR_OF_DAY, 00);
            c.set(Calendar.MINUTE, 00);
            c.set(Calendar.SECOND, 00);
            entity.setCjsj_start(sdf.format(c.getTime()));

            c.setTime(new Date(Long.valueOf(entity.getCjsj_end())));
            c.set(Calendar.HOUR_OF_DAY, 23);
            c.set(Calendar.MINUTE, 59);
            c.set(Calendar.SECOND, 59);
            entity.setCjsj_end(sdf.format(c.getTime()));
            //导出查询
            List<OutstockDetailedReport> outstockList=dao.baseSelectPage(entity,ddqdidList1);
            for(OutstockDetailedReport outstockDetailedReport:outstockList){
                getGongtong(entity, list, outstockDetailedReport);
            }
        }
        return  list;
    }


    /**
     * (导出)表头创建
     * @param hzid
     * @param ckid
     * @return
     */
    public List<ExcelExportEntity> listHeader2(String hzid, String ckid) {
        List<ExcelExportEntity> entity = new ArrayList<ExcelExportEntity>();
        //中文汉字产品编号
        String cpbh= OutstockDetailed.cpbh.getType();
        //中文首字母
        String cpbh2=PinyinUtils.ToPinyin(cpbh);
        entity.add(new ExcelExportEntity(cpbh, cpbh2));
        //产品分类
        String cpfl=OutstockDetailed.cpfl.getType();
        //中文首字母
        String cpfl2=PinyinUtils.ToPinyin(cpfl);
        entity.add(new ExcelExportEntity(cpfl, cpfl2));
        //产品名称
        String cpmc=OutstockDetailed.cpmc.getType();
        //中文首字母
        String cpmc2=PinyinUtils.ToPinyin(cpmc);
        entity.add(new ExcelExportEntity(cpmc, cpmc2));
        //订单渠道
        String ddqdmc=OutstockDetailed.ddqdmc.getType();
        //中文首字母
        String ddqdmc2=PinyinUtils.ToPinyin(ddqdmc);
        entity.add(new ExcelExportEntity(cpmc, ddqdmc2));
        //规格
        String gg=OutstockDetailed.gg.getType();
        String gg2=PinyinUtils.ToPinyin(gg);
        entity.add(new ExcelExportEntity(gg, gg2));
        //单位描述(主单位的)
        String dw=OutstockDetailed.dw.getType();
        String dw2=PinyinUtils.ToPinyin(dw);
        entity.add(new ExcelExportEntity(dw, dw2));
        //期初库存
        String qckc=OutstockDetailed.qckc.getType();
        String qckc2=PinyinUtils.ToPinyin(qckc);
        entity.add(new ExcelExportEntity(qckc, qckc2));
        List<WmsOwnerOrdertype> listType=dao.selectType(hzid,ckid);
        for(WmsOwnerOrdertype wmsOwnerOrdertype:listType){
            //汉字
            String ddlx=wmsOwnerOrdertype.getDdlx();
            //拼音
            String ddlx2=PinyinUtils.ToPinyin(ddlx);
            entity.add(new ExcelExportEntity(ddlx, ddlx2));
        }
        //出库汇总
        String ckhz=OutstockDetailed.ckhz.getType();
        String ckhz2=PinyinUtils.ToPinyin(ckhz);
        entity.add(new ExcelExportEntity(ckhz, ckhz2));
        //期末库存
        String qmkc=OutstockDetailed.qmkc.getType();
        String qmkc2= PinyinUtils.ToPinyin(qmkc);
        entity.add(new ExcelExportEntity(qmkc, qmkc2));
        return entity;
    }
}